tools: ocaml: Fix invalid memory access in OCaml mmap library
authorZheng Li <dev@zheng.li>
Thu, 6 Oct 2011 16:45:43 +0000 (17:45 +0100)
committerZheng Li <dev@zheng.li>
Thu, 6 Oct 2011 16:45:43 +0000 (17:45 +0100)
commitc9e3fb65ae60f2719acdb11ce648620b1d6c6ce4
treef2291082119fed3972f513dfc7af7204dfe1a003
parent8d7a87b20e43fa82feed36abd41abf4f02444c73
tools: ocaml: Fix invalid memory access in OCaml mmap library

Fix invalid memory access in OCaml mmap library (to play nicely with the GC)

This was a bug reported by Roberto Di Cosmo. When he tried to reuse
the mmap library for his own project, Mmap.read occasionally got
different result when reading from the same map. This turned out to be
a bug in the binding, where a C pointer was created pointing to a
OCaml value, and the OCaml value was subsequently moved around by the
GC after memory allocation and hence invalidated the C pointer. This
patch removes the indirection of C pointer and uses OCaml macro to
access values directly.

Only Mmap.read function had this problem. The other functions, despite
having the same code style, didn't have memory allocation involved
hence wouldn't intrigue such an error. I've changed all of them to the
safer style for future proof. Directly casting OCaml value's *data
block* (rather than the value itself) as a C pointer is not a common
practice either, but I'll leave it as it is.

The bug hadn't occured on XenServer because XenServer didn't make use
of the Mmap.read function (except in one place for debugging). In
XenServer, most mmap operations were going through another pair of
separately implemented functions (Xs_ring.read/write).

Signed-off-by: Zheng Li <dev@zheng.li>
Committed-by: Ian Jackson <ian.jackson@eu.citrix.com>
tools/ocaml/libs/mmap/mmap_stubs.c